<?php

namespace app\modules\Supplier\controllers;

use Yii;
use app\modules\Supplier\models\Supplier;
use app\modules\Supplier\models\SupplierSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * 供应商管理
 */
class SupplierController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    //供应商列表
    public function actionIndex()
    {
        $searchModel = new SupplierSearch();

        if(!Yii::$app->user->can('all_supplier')){
            
            $searchModel->nation = \yii::$app->user->identity->nation;
        }
        
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    //供应商详情
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    //创建供应商
    public function actionCreate()
    {
        $model = new Supplier();

        if ($model->load(Yii::$app->request->post())) {

            if (Yii::$app->request->isAjax) {  
  
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;  
      
                return \yii\bootstrap\ActiveForm::validate($model);   
            }

            if($model->type == 5){ //为导游
                $model->is_tour = 2;
                $model->person = '';
                $model->address = '';
            }

            if($model->type != 1){
                $model->hotel_group = 0;
            }

            $model->nation = \yii::$app->user->identity->nation;

            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }else{
            $model->type = 1;
            return $this->render('create', [
                'model' => $model,
            ]);
        }

        
    }


    //更新供应商
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {

            if (Yii::$app->request->isAjax) {  
  
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;  
      
                return \yii\bootstrap\ActiveForm::validate($model);   
            }

            if($model->type == 5){ //为导游
                $model->is_tour = 2;
                $model->person = '';
                $model->address = '';
            }

            if($model->type != 1){
                $model->hotel_group = 0;
            }

            $model->nation = \yii::$app->user->identity->nation;
            
            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            }
            
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

   
    
    protected function findModel($id)
    {
        if (($model = Supplier::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
    
}
